package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.SourcesBusiness;

public class SourcesBusinessLogic extends AbstractBL{
	
	public SourcesBusiness GetSourcesBusiness(long id)
	{
		Key key = KeyFactory.createKey(EntityName.SourcesBusiness, id);
		try {
			Entity lang = datastore.get(key);
			SourcesBusiness c = new SourcesBusiness();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
		
	
	public SourcesBusiness GetSourcesBusiness(long hotelId,String title)
	{
		Query q = new Query(EntityName.SourcesBusiness);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterTitle);
		
		q.setFilter(filter);
		
		return GetSourcesBusiness(datastore.prepare(q).asSingleEntity());
	}
	
	public List<SourcesBusiness> GetAllSourcesBusiness(long hotelId)
	{
		Query q = new Query(EntityName.SourcesBusiness);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,"true");
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,"true");

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public SourcesBusiness GetSourcesBusiness(Entity entity)
	{
		SourcesBusiness item = new SourcesBusiness();
		item.setEntity(entity);
		return item;
	}
	
	public List<SourcesBusiness> GetList(List<Entity> entities)
	{
		List<SourcesBusiness> list = new ArrayList<SourcesBusiness>();
		for(Entity e : entities)
		{
			SourcesBusiness a = new SourcesBusiness();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public SourcesBusiness AddSourcesBusiness(long hotelId,String title,String description, Boolean defaultsource) throws EntityNotFoundException{
		SourcesBusiness t = new SourcesBusiness();
		t.setHotelId(hotelId);
		t.setTitle(title);
		t.setDescription(description);
		t.setAvaliable(true);
		t.setStatus(true);
		t.setDefault_source(defaultsource);
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public SourcesBusiness EditSourcesBusiness(long sourcesbusinessId,String title,String description) throws EntityNotFoundException{
		SourcesBusiness t = GetSourcesBusiness(sourcesbusinessId);
		t.setTitle(title);
		t.setDescription(description);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public SourcesBusiness DeleteSourcesBusiness(long sourcesbusinessId) throws EntityNotFoundException{
		SourcesBusiness t = GetSourcesBusiness(sourcesbusinessId);
		t.setAvaliable(false);
		t.setStatus(false);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public SourcesBusiness ChangeStatus(long sourcesbusinessId) throws EntityNotFoundException{
		
		SourcesBusiness a = GetSourcesBusiness(sourcesbusinessId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}	 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	public List<SourcesBusiness> GetSelectBoxSourcesBusiness(long hotelId)
	{
		Query q = new Query(EntityName.SourcesBusiness);
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		q.setFilter(filterHotel);
		return GetSelectBoxSourcesBuss(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	public List<SourcesBusiness> GetSelectBoxSourcesBuss(List<Entity> entities)
	{
		List<SourcesBusiness> list = new ArrayList<SourcesBusiness>();
		for(Entity e : entities)
		{
			SourcesBusiness a = new SourcesBusiness();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	// tee+
	public SourcesBusiness CreateSourcesBusinessSample() {
		 String[] title = new String[] { "Yahoo", "OnlineReservation", "GuestReferral", "ReturnGuest", "Google"};
		 String[] description= new String[] { "Yahoo", "OnlineReservation", "GuestReferral", "ReturnGuest", "Google"};
		 Boolean[] defaultsource = new Boolean[] { true, true, true , true, true};

		 SourcesBusiness sb = new SourcesBusiness();
		for (int i = 0; i < title.length; i++) {
			sb.setAvaliable(true);
			sb.setHotelId(1);
			sb.setDescription(description[i]);
			sb.setDefault_source(defaultsource[i]);
			sb.setStatus(true);
			sb.setTitle(title[i]);

			Key k = datastore.put(sb.getEntity());
			sb.setId(k.getId());
			sb.setKey(k);

		}
		return null;
	}
	
	public List<SourcesBusiness> GetAllSourcesBusiness4AdminLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.SourcesBusiness);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("Avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}
	
	public List<SourcesBusiness> GetAllSourcesBusiness4Admin(long hotelId)
	{
		Query q = new Query(EntityName.SourcesBusiness);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("Avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
}
